package com.terrier.ui.core
{
    import com.terrier.ui.effects.IWxAbstractEffect;

    import flash.filters.DropShadowFilter;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    /**
     * 默认的工具提示类
     * @author Jarvis.weng
     */
    public class WxDefaultToolTip extends WxComponent implements IWxToolTip
    {
        /**
         * 背景颜色
         * @default
         */
        public static var bgColor:uint = 0x696969;

        /**
         * 背景透明度
         * @default
         */
        public static var bgAlpha:Number = 0.8;

        /**
         * 圆角的弧度
         * @default
         */
        public static var cornerRadius:Number = 10;

        /**
         * 是否具有阴影
         * @default
         */
        public static var hasDropShadow:Boolean = true;

        /**
         * 最大的文本宽度
         * @default
         */
        public static var maxTextWidth:Number = 200;

        /**
         * 文本的左边距
         * @default
         */
        public static var leftMargin:Number = 5;

        /**
         * 文本的右边距
         * @default
         */
        public static var rightMargin:Number = 5;

        /**
         * 文本的上边距
         * @default
         */
        public static var topMargin:Number = 5;

        /**
         * 文本的下边距
         * @default
         */
        public static var bottomMargin:Number = 5;

        /**
         * @private
         */
        protected var _textDisplay:TextField;

        /**
         * 构造函数
         */
        public function WxDefaultToolTip()
        {
            super();
            createChildren();
        }

        /**
         * 提示信息
         * @return
         */
        public function get tipInfo():Object
        {
            return _textDisplay.text;
        }

        /**
         * @private
         */
        public function set tipInfo(value:Object):void
        {
            if (value is String)
            {
                _textDisplay.text = String(value);
            }
            else
            {
                _textDisplay.text = value.toString();
            }

            updateView();
        }

        /**
         * 隐藏特效
         * @return
         */
        public function get hideEffect():IWxAbstractEffect
        {
            return null;
        }

        /**
         * 显示特效
         * @return
         */
        public function get showEffect():IWxAbstractEffect
        {
            return null;
        }

        /**
         * @private
         */
        protected function createChildren():void
        {
            var txtFmt:TextFormat = new TextFormat();
            txtFmt.size = 14;
            txtFmt.color = 0xeeeeee;
            txtFmt.align = TextFormatAlign.JUSTIFY;

            _textDisplay = new TextField();
            _textDisplay.defaultTextFormat = txtFmt;

            _textDisplay.selectable = false;
            _textDisplay.multiline = true;
            _textDisplay.autoSize = TextFieldAutoSize.LEFT;

            addChild(_textDisplay);
        }

        /**
         * @private
         */
        override protected function updateView():void
        {
            measureText();
            drawBackground();
        }

        /**
         * 设置文本内容长度，使之不超过最大宽度，同时更新背景，文本默认靠左，两端对齐
         * @private
         */
        protected function measureText():void
        {
            _textDisplay.wordWrap = false;
            if (_textDisplay.width > maxTextWidth)
            {
                _textDisplay.width = maxTextWidth;
                _textDisplay.wordWrap = true;
            }

            _textDisplay.x = leftMargin;
            _textDisplay.y = topMargin;
        }

        /**
         * 填充背景,添加阴影效果，透明度
         * @private
         */
        protected function drawBackground():void
        {
            graphics.clear();
            graphics.beginFill(bgColor, bgAlpha);
            graphics.drawRoundRect(0,
                                   0,
                                   _textDisplay.width + rightMargin + leftMargin,
                                   _textDisplay.textHeight + bottomMargin + topMargin,
                                   cornerRadius,
                                   cornerRadius);
            graphics.endFill();

            if (hasDropShadow)
            {
                var dsfilter:DropShadowFilter = new DropShadowFilter(4, 45, 0x000000, 0.7);
                filters = [ dsfilter ];
            }
            else
            {
                filters = null;
            }
        }
    }
}
